import java.net.Socket;
import java.awt.Graphics;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class OnlinePokerClientListener implements Runnable {
	
	public Socket ClientSocket;
	public Table table;
	public OnlinePokerClient obj;
	public Player myself = null;
	public Graphics g = null;
	
	OnlinePokerClientListener(Socket sock, Table tab, OnlinePokerClient k)
	{
		this.ClientSocket = sock;
		this.table = tab;
		this.obj = k;
	}
	
	public void Listen()
	{
		System.out.println("Listening");
		try {
			InputStream in = ClientSocket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(in);
			String code;
			while(true)
			{
				try {
					Message m = (Message) ois.readObject();
					
					if(m.Code.equals("SendingTable"))
					{
						table = (Table) m.obj;
						obj.updateTable(table);
					}
					if(m.Code.equals("YourTurn"))
					{
						obj.myTurn = true;
						obj.startTimer();
					}
					if(m.Code.equals("Chat"))
					{
						obj.updateChatWindow((String) m.obj);
					}
					if(m.Code.equals("WinnerDecided"))
					{
						obj.DisplayWinnerInfo((Table) m.obj);
					}
					if(m.Code.equals("TableList"))
					{
						System.out.println((String) m.obj);
						obj.ReceivedTableList((String)m.obj);
					}
					
					
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	
	public void run()
	{
		Listen();
	}
}
